<template>
  <div class="app-container">
    <el-form  :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="120px" label="用户" prop="userId">
        <!-- <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select clearable @change="handleQuery" v-model="queryParams.userId" filterable placeholder="请选择" style="width: 90%">
          <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.nickname"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="120px" label="提现金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入提现金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="提现时间" prop="applicationTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.applicationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择提现时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label-width="120px" label="提现状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择提现状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="120px" label="提现账户" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入提现账户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label-width="120px" label="提现方式" prop="withdrawalMethod">
        <el-select v-model="queryParams.withdrawalMethod" placeholder="请选择提现方式" clearable size="small">
          <el-option
            v-for="dict in withdrawalMethodOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:withdrawOrder:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:withdrawOrder:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:withdrawOrder:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['finance:withdrawOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="withdrawOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户"  width="120" align="center" prop="nickname"  fixed="left"/>
      <el-table-column label="提现卡号" width="200" align="center" prop="cardId" />
      <el-table-column label="真实姓名" width="100" align="center" prop="realName" />
      <el-table-column label="开户行" width="120" align="center" prop="bankName" />
      <el-table-column label="提现状态" width="130" align="center" prop="tag">
        <template slot-scope="scope">
            <el-tag
              v-if="scope.row.stateId === '1'"
              disable-transitions>{{scope.row.state}}</el-tag>
            <el-tag
              v-else-if="scope.row.stateId === '2'"
              type="success"
              disable-transitions>{{scope.row.state}}</el-tag>
            <el-tag
              v-else-if="scope.row.stateId === '3'"
              type="info"
              disable-transitions>{{scope.row.state}}</el-tag>
            <el-tag
              v-else
              type="danger"
              disable-transitions>{{scope.row.state}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" width="100" align="center" prop="money" />
      <el-table-column label="实际到账金额" width="100" align="center" prop="actualarrival" />
      <el-table-column label="提现申请时间" width="170" align="center" prop="applicationTime" />
      <el-table-column label="手续费比例" width="120" align="center" prop="extractRatio" />
      <el-table-column label="手续费" width="100" align="center" prop="extractMoney" />
      <el-table-column label="税收比例" width="120" align="center" prop="revenueRatio" />
      <el-table-column label="税收" width="100" align="center" prop="revenueMoney" />
      <el-table-column label="消耗能量值" width="120" align="center" prop="energy" />
      <el-table-column label="处理人" width="120" align="center" prop="handlePerson" />
      <el-table-column label="处理时间" width="180" align="center" prop="handleTime" />
      <el-table-column label="处理结果" min-width="250" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:withdrawOrder:remove']"
          >已发放</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:withdrawOrder:edit']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 驳回弹框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="驳回理由" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="驳回理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submittingLoding" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSzUserSimple,
  listWithdrawOrder, getWithdrawOrder, delWithdrawOrder, addWithdrawOrder, updateWithdrawOrder, exportWithdrawOrder } from "@/api/finance/withdrawOrder";

export default {
  name: "WithdrawOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 控制确定按钮的 loading 状态
      submittingLoding: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现订单表格数据
      withdrawOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 提现状态字典
      stateOptions: [],
      // 提现方式字典
      withdrawalMethodOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        money: null,
        applicationTime: null,
        state: null,
        account: null,
        withdrawalMethod: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 购买人下拉框
      userOptions:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("DMC006").then(response => {
      this.stateOptions = response.data;
    });
    this.getDicts("DMC003").then(response => {
      this.withdrawalMethodOptions = response.data;
    });

    const formData1 = new FormData();
    formData1.append('userId', null);
    listSzUserSimple(formData1).then(response => {
      this.userOptions = response.data;
    })
  },
  methods: {
    /** 查询提现订单列表 */
    getList() {
      this.loading = true;
      listWithdrawOrder(this.queryParams).then(response => {
        this.withdrawOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        money: null,
        applicationTime: null,
        state: null,
        handleTime: null,
        handlePerson: null,
        remark: null,
        payId: null,
        extractRatio: null,
        revenueRatio: null,
        extractMoney: null,
        revenueMoney: null,
        energy: null,
        account: null,
        withdrawalMethod: null,
        actualarrival: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现订单";
    },
    /** 打开驳回弹框 */
    handleUpdate(row) {
      this.reset();
      this.submittingLoding = false;
      this.title = "提现订单驳回";
      this.form.id = row.id;
      this.open = true;
    },
    /** 驳回 提交按钮 */
    submitForm() {
      this.submittingLoding = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.state = '4';
            updateWithdrawOrder(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 发放 */
    handleDelete(row) {
      this.reset();
      this.form.id = row.id;
      this.$confirm(row.nickname + '提现' + row.money + '元确认已发放吗？', '发放', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.state = '2';
        updateWithdrawOrder(this.form).then(response => {
            this.msgSuccess("操作成功");
            this.getList();
          });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有提现订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportWithdrawOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
